package com.wanxi.version3;

public class NPC implements Attack {
    private String name;
    private int bloodVolume = 100;//血量
    private int physical = 100;//体能
    private int blueAmount = 100;//蓝量

    public NPC() {
    }

    public NPC(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBloodVolume() {
        return bloodVolume;
    }

    public void setBloodVolume(int bloodVolume) {
        this.bloodVolume = bloodVolume;
    }

    public int getPhysical() {
        return physical;
    }

    public void setPhysical(int physical) {
        this.physical = physical;
    }

    public int getBlueAmount() {
        return blueAmount;
    }

    public void setBlueAmount(int blueAmount) {
        this.blueAmount = blueAmount;
    }

    @Override
    public void magicAttack(Object o) {
        Boolean alive = this.aliveObject(o);
        Boolean power = this.myPower(blueAmount);
        if (alive && power) {
            blueAmount = blueAmount - 10;
            this.bloodObject(o);
        }
    }


    @Override
    public void physicalAttack(Object o) {
        Boolean alive = this.aliveObject(o);
        Boolean power = this.myPower(physical);
        if (alive && power) {
            physical = physical - 10;
            this.bloodObject(o);
        }
    }

    /**
     * 判断被攻击的对象类型，并判断对象是否活着。
     */
    public Boolean aliveObject(Object o) {
        Boolean alive = true;
        if (o instanceof Gamer) {
            Gamer gamer = (Gamer) o;
            if (gamer.getBloodVolume() > 0) {
                alive = true;
            } else {
                alive = false;
                System.out.println(gamer.getName() + "已经死亡，无法攻击");
            }
        } else {
            NPC npc = (NPC) o;
            if (npc.getBloodVolume() > 0) {
                alive = true;
            } else {
                alive = false;
                System.out.println(npc.getName() + "已经死亡，无法攻击");
            }
        }
        return alive;
    }

    /**
     * 判断自己的能量是否够用。
     */
    public Boolean myPower(int type) {
        Boolean power = true;
        if (type >= 10) {
            power = true;
        } else {
            power = false;
            System.out.println(this.getName() + "能量不足，无法攻击");
        }
        return power;
    }

    /**
     * 攻击后，减掉被攻击对象的生命，并判断是否死亡，死亡输出对方死亡。
     */
    public void bloodObject(Object o) {
        if (o instanceof Gamer) {
            Gamer gamer = (Gamer) o;
            gamer.setBloodVolume(gamer.getBloodVolume() - 20);
            if (gamer.getBloodVolume() <= 0) {
                System.out.println(gamer.getName() + "已经死亡");
            }
        } else {
            NPC npc = (NPC) o;
            npc.setBloodVolume(npc.getBloodVolume() - 20);
            if (npc.getBloodVolume() <= 0) {
                System.out.println(npc.getName() + "已经死亡");
            }
        }
    }



    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("NPC{");
        sb.append("name='").append(name).append('\'');
        sb.append(", bloodVolume=").append(bloodVolume);
        sb.append(", physical=").append(physical);
        sb.append(", blueAmount=").append(blueAmount);
        sb.append('}');
        return sb.toString();
    }
}
